package com.example.my_project.auto_v2.service.impl;

import com.example.my_project.auto_v2.mapper.BodyEntityMapper;
import com.example.my_project.auto_v2.mapper.ParamEntityMapper;
import com.example.my_project.auto_v2.mapper.PathEntityMapper;
import com.example.my_project.auto_v2.pojo.HttpEntity;
import com.example.my_project.auto_v2.pojo.JsonResult;
import com.example.my_project.auto_v2.pojo.ParamEntity;
import com.example.my_project.auto_v2.pojo.PathEntity;
import com.example.my_project.auto_v2.service.IPathService;
import com.example.my_project.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: chenbo
 * @Description:
 * @Date: Created in 14:39 2018/7/25
 * @Modified By:
 */
@Service
public class IPathServiceImpl implements IPathService {

    @Autowired
    private PathEntityMapper pathEntityMapper;

    @Autowired
    private BodyEntityMapper bodyEntityMapper;

    @Autowired
    private ParamEntityMapper paramEntityMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonResult savePath(PathEntity entity) {

        if ( entity==null || StringUtils.isEmpty(entity.getVersion())||StringUtils.isEmpty(entity.getPath())) {
            return JsonResult.errorMsg("请求参数为空！");
        }
        PathEntity pathAndVersion = pathEntityMapper.findCountByPathAndVersion(entity);

        if ( pathAndVersion != null ) {
            return JsonResult.pass("不能插入重复的数据！",pathAndVersion);
        }else {
            try {
                pathEntityMapper.insert(entity);
            }catch ( Exception e ){
                e.printStackTrace();
                return JsonResult.exceptionMsg("数据库连接失败!",e.getMessage());
            }
            return JsonResult.pass("保存成功",pathEntityMapper.findCountByPathAndVersion(entity));
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonResult findAll() {
        List<PathEntity> entityList = pathEntityMapper.selectAll();
        return JsonResult.pass(entityList);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public JsonResult findApi() {
        List<PathEntity> entityList = pathEntityMapper.selectAll();
        if (entityList.isEmpty()) {
            return JsonResult.pass(entityList);
        }else {
            List<HttpEntity> httpList = new ArrayList<>();
                for (PathEntity pathEntity : entityList) {

                    HttpEntity httpEntity = new HttpEntity();
                    Integer id = pathEntity.getId();

                    httpEntity.setPath(pathEntity);

                    httpEntity.setBody(bodyEntityMapper.findByPathId(id));

                    List<ParamEntity> byPathId = paramEntityMapper.findByPathId(id);
                    if (byPathId.isEmpty()) {

                    }else {
                        for (ParamEntity paramEntity : byPathId) {
                            System.out.println(paramEntity.toString());
                        }
                    }
                    httpEntity.setParamList(byPathId);

                    httpList.add(httpEntity);
                }
                return JsonResult.pass(httpList);
        }
    }

    @Override
    public JsonResult saveApi(HttpEntity entity) {
        JsonResult pathResult = savePath(entity.getPath());
        if (pathResult.getCode()==0) {
            PathEntity pathEntity = (PathEntity) pathResult.getValue();

            List<ParamEntity> paramList = entity.getParamList();
            if ( paramList != null && !paramList.isEmpty()) {

            }


        }else {
            return pathResult;
        }
        return null;
    }
}
